package com.hiveway.mapper;

import com.github.pagehelper.Page;
import com.hiveway.dto.GoodsSalesDTO;
import com.hiveway.dto.OrdersPageQueryDTO;
import com.hiveway.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据
     *
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 历史订单查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    @Update("update orders set " +
            "status = #{status}, pay_status = #{payStatus}, cancel_time = #{cancelTime}, cancel_reason = #{cancelReason}, rejection_reason = #{rejectionReason} " +
            "where id = #{id}")
    void update(Orders orders);

    /**
     * 各个状态的订单数量统计
     *
     * @param status
     * @return
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer getStatusNum(Integer status);

    /**
     * 处理订单长时间未支付或派送状态异常
     *
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    /**
     * 根据动态条件查询每日营业额
     *
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件查询订单总量与有效的订单总量
     *
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据动态条件查询销量前十的商品
     *
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> gettop10(LocalDateTime begin, LocalDateTime end);
}
